package com.systemdevelopment.mainproject.View.Admin;

import com.jfoenix.controls.JFXButton;
import com.systemdevelopment.mainproject.Model.Book;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;

public class configBookController extends newBookController{

    @FXML
    JFXButton giveUpButton;
    @FXML
    JFXButton configBookButton;
    Book b;
    @FXML
    public void configBook(ActionEvent actionEvent) {
        b.setName(bookName.getText());
        b.setAuthor(authors);
        b.setPublicationDate(publishDate.getValue().toString());
        b.setPublisher(publisher.getText());
        b.setAllQuantity(Integer.parseInt(quantityField.getText()));
        b.setNowQuantity(Integer.parseInt(quantityField.getText()));
        try {
            Book.DeleteBookFromDB(b.getISBN());
            Book.InsertBookIntoDB(b);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void giveUp(ActionEvent actionEvent) {
    }
    public void loadBook(Book b)
    {
        this.b=b;
        bookName.setText(b.getName());
        publisher.setText(b.getPublisher());
        ISBN.setText(b.getISBN());
        try {
            if(b.getPublicationDate()!=null)
            publishDate.setValue(LocalDate.parse(b.getPublicationDate()));
        }catch (DateTimeParseException e)
        {
            System.out.println("日期错误");
        }
        selectedAuthors.setItems(FXCollections.observableArrayList(b.getAuthor()));
        quantityField.setText(""+b.getAllQuantity());
    }
}
